<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h2>sessionStorage</h2>
    <button onclick="saveData()">点我保存数据</button>
    <button onclick="readData()">点我读取数据</button>
    <button onclick="deleteData()">点我删除person数据</button>
    <button onclick="clearData()">点我清空数据</button>

    <script>
        //sessionStorage存在浏览器中，关闭浏览器之后清除
        function saveData() {
            //sessionStorage位于window对象，可直接写
            //key不存在时会添加，存在时会替换，value只能存string
            window.sessionStorage.setItem('msg', 'hello')
            //存储任何类型，value都会调用toString
            sessionStorage.setItem('msg2', 666)
            //存储对象要存json格式字符串
            let p = {name: 'zs', age: 19}
            sessionStorage.setItem('person', JSON.stringify(p))
        }

        function readData() {
            console.log(sessionStorage.getItem('msg'))
            console.log(sessionStorage.getItem('msg2'))
            //存储json格式字符串要解析
            console.log(JSON.parse(sessionStorage.getItem('person')))
            //getItem的key如果不存在，返回值是null
            console.log(sessionStorage.getItem('ne'))
        }

        function deleteData() {
            sessionStorage.removeItem('person')
        }

        function clearData() {
            sessionStorage.clear()
        }
    </script>
</body>
</html>